package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.SearchResultAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.ClassifyGoodBean;
import cn.com.ddapp.ddm.bean.SearchResultBean;
import cn.com.ddapp.ddm.customview.MySearchFlowLayout;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/8/24 0024.//搜素
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.imageButton)
    Button imageButton;
    @BindView(R.id.clearText)
    TextView clearText;
    @BindView(R.id.flowlayout)
    MySearchFlowLayout flowlayout;
    @BindView(R.id.qingchu)
    ImageView qingchu;
    @BindView(R.id.search_nothing)
    TextView search_nothing;
    @BindView(R.id.search_result_list)
    ListView search_result_list;
    @BindView(R.id.ser_img)
    ImageView ser_img;
    @BindView(R.id.empty)
    EmptyLayout emptyLayout;
    private boolean booleanData;
    private TextView textView1;
    private List<ClassifyGoodBean.ClassifyGood> nList;
    private SearchResultAdapter sAdapter;
    private List<String> rList;

    @Override
    public void initView() {
        setTitle("搜索");
        setTitleLeftImg(R.mipmap.back_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               finish();
            }
        });

    }

    @Override
    public void initData() {
//        booleanData = SPUtils.getBooleanData(getApplicationContext(), SPUtils.ISText);
//        if(booleanData==true){
//            initDataText(textView);
//        }
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.toString() == null || "".equals(s.toString())){
                    ser_img.setVisibility(View.VISIBLE);
                }else{
                    ser_img.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TDevice.isTablet(getApplicationContext())){
                    reequestLandData(editText.getText().toString());
                }else{
                   reequestData(editText.getText().toString());
                }
            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_search;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.imageButton, R.id.clearText,R.id.qingchu,R.id.img_error_layout})
    @Override
    public void onClick(View view) {
        String content=editText.getText().toString();
        switch (view.getId()) {
            case R.id.imageButton:
//                addView();
                if(TextUtils.isEmpty(content)){
                    BaseApplication.showToast("请输入搜索的内容");
                    editText.requestFocus();
                }else{
                    if(TDevice.isTablet(getApplicationContext())){
                        Intent intent=new Intent(SearchActivity.this,GoodLandClassifyActivity.class);
                        intent.putExtra("searchResult",editText.getText().toString());
                        startActivity(intent);
                        finish();
                    }else{
                        Intent intent=new Intent(SearchActivity.this,GoodClassifyActivity.class);
                        intent.putExtra("searchResult",editText.getText().toString());
                        startActivity(intent);
                        finish();
                    }
                }
                break;

            case R.id.clearText:
                flowlayout.removeAllViews();
                break;
            case R.id.qingchu:
                editText.setText("");
                break;
            case R.id.img_error_layout:
                if(TDevice.isTablet(getApplicationContext())){
                    reequestLandData(content);
                }else{
                    reequestData(content);
                }
                break;
        }
    }

    private void reequestLandData(String content) {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback = new OkUtilCallback(emptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                SearchResultBean entity = gson.fromJson(response, SearchResultBean.class);
                if(entity.getResult().equals("OK") && entity.getList().size()==0){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    search_nothing.setVisibility(View.GONE);
                }else{
                    search_nothing.setVisibility(View.GONE);
                    rList=entity.getList();
                    sAdapter=new SearchResultAdapter(SearchActivity.this,rList);
                    search_result_list.setAdapter(sAdapter);
                    search_result_list.setOnItemClickListener(slandListener);
                }
            }

        };
        String params="key="+content;
        OKUtilAPI.post(OKUtilAPI.goodsearch, params, okUtilCallback);
    }

    private void reequestData(final String context) {
        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
        final OkUtilCallback okUtilCallback = new OkUtilCallback(emptyLayout) {
            @Override
            public void onMyResponse(String response, int id) {
                SearchResultBean entity = gson.fromJson(response, SearchResultBean.class);
                if(entity.getResult().equals("OK") && entity.getList().size()==0){
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                    search_nothing.setVisibility(View.GONE);
                }else{
                    search_nothing.setVisibility(View.GONE);
                    rList=entity.getList();
                    sAdapter=new SearchResultAdapter(SearchActivity.this,rList);
                    search_result_list.setAdapter(sAdapter);
                    search_result_list.setOnItemClickListener(sListener);
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            }
        };
        String params="key="+context;
        OKUtilAPI.post(OKUtilAPI.goodsearch, params, okUtilCallback);
    }

    public AdapterView.OnItemClickListener slandListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String name=rList.get(i);
            Intent intent=new Intent(SearchActivity.this,GoodLandClassifyActivity.class);
            intent.putExtra("searchResult",name);
            startActivity(intent);
            finish();
        }
    };
    public AdapterView.OnItemClickListener sListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String name=rList.get(i);
            Log.i("huaG" ,"serachname == "+name);
            Intent intent=new Intent(SearchActivity.this,GoodClassifyActivity.class);
            intent.putExtra("searchResult",name);
            startActivity(intent);
            finish();
        }
    };
    private void addView() {
        if (editText.getText().length() == 0) {
                qingchu.setVisibility(View.INVISIBLE);
        } else {
            qingchu.setVisibility(View.VISIBLE);
            MySearchFlowLayout.MarginLayoutParams params = new MySearchFlowLayout.MarginLayoutParams(
                    MySearchFlowLayout.LayoutParams.WRAP_CONTENT,
                    MySearchFlowLayout.LayoutParams.WRAP_CONTENT
            );
            params.leftMargin = 20;
            params.topMargin = 20;
            params.bottomMargin = 20;
            params.rightMargin = 20;

            TextView textView = new TextView(SearchActivity.this);
            textView.setBackground(BaseApplication.getIntstance().getResources().getDrawable(R.drawable.black_shape));
            textView.setTextColor(BaseApplication.getIntstance().getResources().getColor(R.color.black));
//            AppConfig.Content= editText.getText()+"";
            initDataText(textView);
            textView.setText(editText.getText());
            textView.setLayoutParams(params);
            textView.setPadding(20, 10, 20, 10);
            flowlayout.addView(textView);
        }
    }

    private void initDataText(TextView textView) {
//        textView.setText( AppConfig.Content);
        SPUtils.setBooleanData(getApplicationContext(), SPUtils.ISText, true);
    }
}
